package com.xm.xmapp.test;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CircleView extends View {
	/** 路线 */
	private Path mPath;
	/** 画笔 */
	private Paint mPaint;

	public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

	}

	public CircleView(Context context) {
		super(context);

	}

	public CircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPath = new Path();
		mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		// mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		mPaint.setStrokeWidth(5);
		mPaint.setColor(Color.RED);
		// 去除锯齿
		mPaint.setAntiAlias(true);
		// 防抖
		mPaint.setDither(true);

		CircleCenter = new Point(400, 400);
	}

	private Point CircleCenter;
	/** 当前圆的半径 */
	private int CircleRadius = 60;
	private float towCircle = 0;
	/** 属性动画 */
	private ObjectAnimator animator;

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		canvas.drawCircle(CircleCenter.x, CircleCenter.y, CircleRadius, mPaint);
		mPath.reset();
		mPath.moveTo(CircleCenter.x, CircleCenter.y - CircleRadius);
		mPath.quadTo(CircleCenter.x + towCircle / 2, CircleCenter.y,
				CircleCenter.x + towCircle, CircleCenter.y - CircleRadius);
		mPath.lineTo(CircleCenter.x + towCircle, CircleCenter.y + CircleRadius);

		mPath.moveTo(CircleCenter.x, CircleCenter.y + CircleRadius);
		mPath.quadTo(CircleCenter.x + towCircle / 2, CircleCenter.y,
				CircleCenter.x + towCircle, CircleCenter.y + CircleRadius);
		mPath.lineTo(CircleCenter.x + towCircle, CircleCenter.y - CircleRadius);
		canvas.drawPath(mPath, mPaint);
		canvas.drawCircle(CircleCenter.x + towCircle, CircleCenter.y,
				CircleRadius, mPaint);
	}

	float firstX, lastX;

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:

			if (isOnClick(event)) {
				firstX = event.getX();
				return true;
			} else
				return false;

		case MotionEvent.ACTION_MOVE:

			lastX = event.getX();
			towCircle = lastX - firstX;
			invalidate();
			break;

		case MotionEvent.ACTION_UP:

			animator = ObjectAnimator.ofFloat(this, "towCircle", 0, 1.0f);
			animator.setDuration((int) (1000));
			animator.start();

			break;

		default:
			break;
		}

		return false;
	}

	public void setTowCircle(float f) {

		if (this.towCircle == 0 && f==1f)
			return;

		this.towCircle = towCircle * (1-f);
		invalidate();
		
	}

	private boolean isOnClick(MotionEvent event) {
		if (event.getX() <= (CircleCenter.x + CircleRadius)
				&& event.getX() >= (CircleCenter.x - CircleRadius)
				&& event.getY() <= (CircleCenter.y + CircleRadius)
				&& event.getY() >= (CircleCenter.y - CircleRadius))
			return true;
		return false;
	}

}
